<?php

class PhpFox_Mod_Menu_ServiceMenu extends BaseService
{
	function _add($aVar, $bParseUrl = true)
	{
		$aQuery = array();
		$aQuery['title_menu'] = (isset($aVar['title_menu']) ? strip_tags($aVar['title_menu']) : '');
		$aQuery['location'] = (isset($aVar['location']) ? $aVar['location'] : '');
		$aQuery['url'] = (isset($aVar['url']) ? ($bParseUrl ? $this->_parseUrl($aVar['url']) : $aVar['url']) : '');
		$aQuery['user_id'] = App::getUser('id');
		$aQuery['is_module'] = (isset($aVar['is_module']) ? $aVar['is_module'] : '');
		$aQuery['parent'] = (isset($aVar['parent']) ? $aVar['parent'] : '');
		$aQuery['mem_only'] = (isset($aVar['mem_only']) ? $aVar['mem_only'] : '');
		$aQuery['product'] = (isset($aVar['product']) ? $aVar['product'] : thisProduct);

		$this->oDatabase->insert(App::getT('menu'), $aQuery);

		$this->_cacheMenu(true);

		return true;
	}

	function _parseUrl($sString)
	{
		$sString = strip_tags($sString);
		$sString = preg_replace( '/ +/', '-',preg_replace('/[^0-9a-zA-Z ]+/', '-', ($sString)));
		return $sString;
	}

	function _update($aVar, $iId, $bParseUrl = true)
	{
		$aData = array();
		$aData['title_menu'] = strip_tags($aVar['title_menu']);
		$aData['location'] = $aVar['location'];
		$aData['url'] = ($bParseUrl ? $this->_parseUrl($aVar['url']) : $aVar['url']);
		$aData['is_module'] = (isset($aVar['is_module']) ? $aVar['is_module'] : '');
		$aData['mem_only'] = (isset($aVar['mem_only']) ? $aVar['mem_only'] : '');
		$this->oDatabase->update(App::getT('menu'), $aData, " menu_id = '". $iId ."' ");

		$this->_cacheMenu(true);

		return true;
	}

	function _delete($iId, $sQuery)
	{
		$this->oDatabase->_delete(App::getT("menu"), " ". $sQuery ." = '". $iId ."' ");

		$this->_cacheMenu(true);
	}

	function _checkUrl($sTitle)
	{
		$aCheck = $this->oDatabase->getRow("SELECT menu_id FROM ". App::getT('menu') ." WHERE url = '". $this->oDatabase->escape($sTitle) ."'");
		if ( !empty($aCheck['menu_id']) )
		{
			return false;
		}
		return true;
	}

	function _cacheMenu($bReCache = false)
	{
		global $_CONF;

		$oCache = &App::newObject("Cache");

		if ( $bReCache )
		{
			$oCache->rmFile("menus.php");
			$oCache->clearTemplates();
		}

   	 	$oCache->setFile("menus.php");

		if ( !$oCache->isCached() )
   	 	{
			$sMenus = "<?php\n";
			$aMenu = $this->oDatabase->getRows("SELECT m.title_menu, m.url, m.location, m.is_module, m.module, m.parent, m.mem_only, s.val, l.phraseid FROM
				". App::getT('menu') ." AS m
					LEFT JOIN ". App::getT('sys_sett') ." AS s ON (m.module = s.code)
					LEFT JOIN " . App::getT('language_phrase') . " AS l ON(m.title_menu = l.varname)
				GROUP BY m.title_menu
				ORDER BY m.listing
			");
			
			$sMenus .= '
			$bTroubleMusic = false;
			if (file_exists(App::getParam(\'path\').\'site/public/music.php\'))
	        {
	            $bTroubleMusic = true;
	        }
            if (!App::checkUploadParams(App::getParam(\'Mod_Music.max_song_size\')))
            {
                $bTroubleMusic = true;
			}
			';

			foreach($aMenu as $aM)
			{
				$bShutDown = "false";
				if ( $aM['val'] )
				{
					$bShutDown = "true";
				}
				if ( $aM['title_menu'] == "menu.public.music" )
				{
					$sMenus .= '$_PHPFOX[\'menu_'. $aM['location'] .'\'][]= array
					(
						\'title\' => \''. str_replace("'", "\'", $aM['title_menu']) .'\',
						\'url\' => \''. $aM['url'] .'\',
						\'is_module\' => \''. $aM['is_module'] .'\',
						\'shutdown\' => '. ($bShutDown == "true" ? $bShutDown : '$bTroubleMusic') .',
						\'mem_only\' => \''. $aM['mem_only'] .'\',
						\'parent\' => \''. $aM['parent'] .'\',
					);' . "\n";
				}
				else
				{
					/*if (!$aM['is_module'] && $aM['phraseid'])
					{
						$aM['is_module'] = '1';
					}*/
					
					$sMenus .= '$_PHPFOX[\'menu_'. $aM['location'] .'\'][]= array
					(
						\'title\' => \''. str_replace("'", "\'", $aM['title_menu']) .'\',
						\'url\' => \''. $aM['url'] .'\',
						\'is_module\' => \''. $aM['is_module'] .'\',
						\'shutdown\' => '. $bShutDown .',
						\'mem_only\' => \''. $aM['mem_only'] .'\',
						\'parent\' => \''. $aM['parent'] .'\',
					);' . "\n";
				}
			}
			$sMenus .= "?>";
			$oCache->createCached($sMenus);
			$oCache->clearTemplates();
		}

		return $oCache->doRequire('_PHPFOX');
	}

	function _getMenus()
	{
		$aMenus = $this->oDatabase->getRows("SELECT * FROM ". App::getT('menu') ." WHERE parent = 0 ORDER BY listing");
		return $aMenus;
	}

	function _updateCount($iId, $iCount)
	{
		$this->oDatabase->update(App::getT('menu'), array('listing' => $iCount), " menu_id = '". $iId ."' ");

		$this->_cacheMenu(true);
	}

	function _deleteMenu($iId)
	{
		$this->oDatabase->_delete(App::getT("menu"), " menu_id = '". $iId ."' ");

		$this->_cacheMenu(true);

		return true;
	}

	function _edit($iId)
	{
		$aRow = $this->oDatabase->getRow("SELECT * FROM ". App::getT('menu') ."
			WHERE menu_id = '". $iId ."'");

		if ( !isset($aRow['menu_id']) )
		{
			return;
		}

		return $aRow;
	}
}

?>